package com.ks.sgms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ks.sgms.dao.AdminDao;

@WebServlet("/regist")
public class RegistServlet extends HttpServlet{
	 @Override
	    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    	resp.setContentType("text/html;charset=utf-8");
			req.setCharacterEncoding("utf-8");
			String user_name = req.getParameter("user_name");
			String password = req.getParameter("password");
			String repassword = req.getParameter("repassword");
			
			//验证两次输入的密码是否一致
			if(!password.equals(repassword)) {
				req.getSession().setAttribute("message", "两次输入的密码不一致！");
	            req.getRequestDispatcher("regist.jsp").forward(req, resp);
	            return;
			}
			
			AdminDao dao = new AdminDao();
			boolean registerCheck = dao.register(user_name, password);
			if (registerCheck) {
	            req.getSession().setAttribute("message", "注册成功！");
				req.getRequestDispatcher("login.jsp").forward(req, resp);
			} else {
	            req.getSession().setAttribute("message",  "注册失败请检查！");
	            req.getRequestDispatcher("regist.jsp").forward(req, resp);
	            
			}
			
	    }
}
